package controllers.manage.user;

import controllers.BaseController;
import jodd.util.StringUtil;
import models.weixin.WebUser;
import order.OrderItem;
import org.apache.commons.lang.StringUtils;
import play.Logger;
import play.modules.paginate.JPAExtPaginator;
import play.mvc.Controller;
import play.mvc.With;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hanbin on 17/5/10.
 */
@With(BaseController.class)
public class WebUserCtrl extends Controller {
    public static final Integer PAGE_SIZE = 10;
    public static void index(Integer pageNumber, String nickName, String phone){
        pageNumber = pageNumber == null ? 1 : pageNumber;
        Integer pageCurrent = pageNumber;
        pageCurrent = (pageCurrent-1)*PAGE_SIZE;


        Map<String , Object> searchMap = new HashMap<>();

        if(StringUtils.isNotBlank(nickName)){
            searchMap.put("nickName", "%"+nickName+"%");
        }


        if(StringUtils.isNotBlank(phone)){
            searchMap.put("phone", phone);
        }

        flash.put("menuId", "4");
        Logger.info("pageCurrent------------ : %s",pageCurrent);
        List<WebUser> resultPage = WebUser.getListByWebUser(nickName,phone,pageCurrent,PAGE_SIZE);
        List<WebUser> allList = WebUser.getListByWebUser(nickName,phone,null,null);
        long webUserNumbers = 2000 ;
        if(allList != null && allList.size()>0)
            webUserNumbers = webUserNumbers+allList.size();

        Logger.info("resultPage.size------------ : %s",resultPage != null ? resultPage.size(): 0);
        int pageCount = 0;
        Logger.info("allList------------ : %s",allList != null ? allList.size(): 0);
        pageCount = allList.size() / PAGE_SIZE;
        if(pageCount != 0 && allList.size() % PAGE_SIZE != 0){
            pageCount = pageCount +1;
        }

        Logger.info("pageNumber------------ : %s",pageNumber);
        Logger.info("pageCount------------ : %s",pageCount);

        render(resultPage, pageNumber, nickName, phone,pageCount , webUserNumbers);

    }


    public static void buyIndex(Integer pageNumber,Long userId,String goodsName){
        pageNumber = pageNumber == null ? 1 : pageNumber;
        Map<String , Object> searchMap = new HashMap<>();
        if(userId != null && userId != 0){
            searchMap.put("userId", userId);
            if(StringUtil.isNotBlank(goodsName)) {
                searchMap.put("goodsName", "%"+goodsName+"%" );
            }
            String sumAmount = "0";
            JPAExtPaginator<OrderItem> resultPage = OrderItem.findByCondition(searchMap, "createdAt desc", pageNumber, PAGE_SIZE);
            List<Map<String,Object>> sumMoneyList = OrderItem.findBuySumByUserId(userId);
            for(Map<String,Object> map : sumMoneyList){
                 sumAmount = map.get("sumAmount").toString();
            }

            render(resultPage, pageNumber,goodsName,userId,sumAmount);
        }
        flash.put("msg" , "用户信息获取不成功!");
        redirect("/BS0008/admin/webuser/index/1");

    }





}
